package com.polaris.live.dialog

import android.content.Context
import android.view.View
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.util.XPopupUtils
import com.polaris.live.R
import com.polaris.live.common.R.color
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.DialogSevenLimitedTimeBinding
import com.polaris.live.manager.GooglePayManager
import com.polaris.live.manager.LiveStateManager
import com.polaris.live.resp.back_resp.PayerRechargeWeekPackPriceResp
import com.polaris.live.utils.image.ImageUtils

/**
 * SevenLimitedTimeDialog
 *
 * @author Created by 半仙 on 2024/4/9/009 14:25
 */
class SevenLimitedTimeDialog(
    context: Context,
    private val mData: List<PayerRechargeWeekPackPriceResp>,
    private val mOnRechargeOk: ((type: Boolean) -> Unit)?,
) : CenterPopupView(context) {
    override fun getImplLayoutId() = R.layout.dialog_seven_limited_time

    private lateinit var mBinding: DialogSevenLimitedTimeBinding

    private var mRechargeOk = false

    override fun onCreate() {
        super.onCreate()
        mBinding = DialogSevenLimitedTimeBinding.bind(popupImplView)
        val item = mData.getOrNull(0) ?: return

        mBinding.apply {
            sevenTvBuy.text = "$${item.price}"
            Glide.with(context).load(ImageUtils.imgPath(item.avatarFrame)).into(ivNewPeopleHead)
            tvNewPeopleHeadDay.text = "${item.avatarFrameDays}day"
            tvGameCoin.text = "${item.gameCoin}"

            val span = SpanUtils.with(tvDiamondCount)
                .append("${item.coin}")
                .setForegroundColor(ColorUtils.getColor(color.color_a7a8ad))
                .append("+${item.giveCoin}")
                .setForegroundColor(ColorUtils.getColor(color.color_FA3A9E))
                .create()
            tvDiamondCount.text = span
            tvDiamondAllCount.text = "${item.coin + item.giveCoin}"

            if (AppCodeUtils.isAR()) {
                shOtherTv.visibility = View.GONE
                shArTv.visibility = View.VISIBLE
            } else {
                shOtherTv.visibility = View.VISIBLE
                shArTv.visibility = View.GONE
            }
        }

        mBinding.sevenTvBuy.setOnSingleClickListener {
            ViewUtils.getTopActivity(context)?.apply {
                GooglePayManager.buyShow(this,
                    item.priceId,
                    UserConst.From.UNKNOWN,
                    item.payerRechargeRewardsType,
                    price = item.price,
                    onSuccess = {
                        mRechargeOk = true
                        dismiss()
                    })
            }
        }

        mBinding.sevenIvClose.setOnSingleClickListener {
            dismiss()
        }
    }

    override fun getMaxWidth(): Int {
        if (popupInfo == null) return 0
        return if (popupInfo.maxWidth == 0) (XPopupUtils.getAppWidth(context) * 1f).toInt() else popupInfo.maxWidth
    }

    override fun onShow() {
        LiveStateManager.updateRechargeState(true)
        LiveStateManager.updataFastRechargeState(true)
        super.onShow()
    }

    override fun onDismiss() {
        mOnRechargeOk?.invoke(mRechargeOk)
        LiveStateManager.updataFastRechargeState(false)
        LiveStateManager.updateRechargeState(false)
        super.onDismiss()
    }

    override fun onDestroy() {
        LiveStateManager.updateRechargeState(false)
        LiveStateManager.updataFastRechargeState(false)
        super.onDestroy()
    }
}